package com.tsd.system.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysGroupRoleResourceMapper;
import com.tsd.system.dao.SysUserMapper;
import com.tsd.system.dao.SysUserRoleMapper;
import com.tsd.system.entity.SysGroupRoleResource;
import com.tsd.system.entity.SysGroupRoleResourceExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserRoleExt;
import com.tsd.system.service.SysGroupRoleResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupRoleResource 服务实现层
 *
 * @author huawei
 * @date 2020/04/16 12:51:37
 */
@Service
public class SysGroupRoleResourceServiceImpl extends BaseServiceImpl implements SysGroupRoleResourceService {

    @Resource
    private SysGroupRoleResourceMapper sysGroupRoleResourceMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public SysGroupRoleResource getSysGroupRoleResourceById(Long id) throws Exception {
        return sysGroupRoleResourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysGroupRoleResourceExt queryBySid(String sid) throws Exception {
        return sysGroupRoleResourceMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysGroupRoleResource(Long id, SysUser opUser) throws Exception {
        SysGroupRoleResource old = sysGroupRoleResourceMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        sysGroupRoleResourceMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveSysGroupRoleResource(SysGroupRoleResource record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            sysGroupRoleResourceMapper.insertSelective(record);
        } else {
            sysGroupRoleResourceMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysGroupRoleResourceExt> findSysGroupRoleResources(Map<String, Object> params, PageBean page) throws Exception {
        return sysGroupRoleResourceMapper.selectByPage(params, page);
    }

    @Override
    public List<SysGroupRoleResourceExt> findSysGroupRoleResources(Map<String, Object> params) throws Exception {
        return sysGroupRoleResourceMapper.selectByParams(params);
    }

    @Override
    public void saveSysResourceList2GroupRole(SysUser opUser, String userId, List<String> resourceIdList) {
        SysUser sysUser = sysUserMapper.selectBySid(userId);
        Map<String, Object> params = new HashMap<>(8);
        params.put("userSid", sysUser.getSid());
        List<SysUserRoleExt> roles = sysUserRoleMapper.selectByUserSid(params);
        if (roles != null && roles.size() > 0) {
//            sysGroupRoleResourceMapper.deleteByGroupSidRoleSid(sysUser.getGroupSid(), roles.get(0).getRoleSid());
//            for (String resId : resourceIdList) {
//                SysGroupRoleResourceExt t = new SysGroupRoleResourceExt();
//                t.setGroup_sid(sysUser.getGroupSid());
//                t.setRes_sid(resId);
//                t.setRole_sid(roles.get(0).getRoleSid());
//                sysGroupRoleResourceMapper.insert(t);
//            }
            // 2020-09-27 改为批量操作
            SysGroupRoleResourceExt resourceExt = new SysGroupRoleResourceExt();
            resourceExt.setGroup_sid(sysUser.getGroup_sid());
            resourceExt.setRole_sid(roles.get(0).getRole_sid());
            List<SysGroupRoleResourceExt> oldList = sysGroupRoleResourceMapper.selectByEntity(resourceExt);

            List<Long> existIdList = new ArrayList<>();
            List<SysGroupRoleResourceExt> insertList = new ArrayList<>();
            for (String resId : resourceIdList) {
                SysGroupRoleResourceExt oldData = ListUtil.get(oldList, t -> resId.equals(t.getRes_sid()));
                if (oldData != null) {
                    existIdList.add(oldData.getId());
                } else {
                    SysGroupRoleResourceExt t = new SysGroupRoleResourceExt();
                    t.setGroup_sid(sysUser.getGroup_sid());
                    t.setRole_sid(roles.get(0).getRole_sid());
                    t.setRes_sid(resId);
                    insertList.add(t);
                }
            }

            List<SysGroupRoleResourceExt> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
            if (!HlpUtils.isEmptyList(delList)) {
                List<Long> deleteIdList = ListUtil.map(delList, SysGroupRoleResourceExt::getId);
                if (!HlpUtils.isEmptyList(deleteIdList)) {
                    BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo();
                    logicVo.setIdList(deleteIdList);
                    sysGroupRoleResourceMapper.deleteByIdList(logicVo);
                }
            }

            if (!HlpUtils.isEmptyList(insertList)) {
                List<SysGroupRoleResourceExt> extList = new ArrayList<>();
                for (SysGroupRoleResourceExt ext : insertList) {
                    extList.add(ext);
                    if (extList.size() == 200) {
                        sysGroupRoleResourceMapper.insertList(extList);
                        extList.clear();
                    }
                }
                if (!HlpUtils.isEmptyList(extList)) {
                    sysGroupRoleResourceMapper.insertList(extList);
                }
            }
        }
    }
}
